<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formContentsTrait{
    public function index_contents(Content $content)
    {
        return $content->title('内容源（不可用）')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_contents()->render()}</div>
HTML
            );
    }
    protected function form_contents(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->html(function () {
            return '';
        }, '<b>一、minisns</b>');
        $form->text('admin_auth_code', '安全码')->default(Settings::get('admin_auth_code', 'asldfafksdzodfansdkfjhashfas'));
        $form->html(function () {
            return '<div style="background-color: powderblue;padding: 10px;border-radius: 5px;">简单说明一下: <br>1、根据同步列表里配置的映射组id对应的映射组，minisns和其他cms进行同步，只有在映射组的圈子才会同步到其他cms，同时反向同步也是根据这里配置的同步规则。<br>2、左边是mini的，右边是其他cms的，如果其他cms有多个分类要同步到mini的同一个圈子，右边可以配置多个。<br>3、<a style="color: blue" href="https://img.mini.minisns.cn/icons/example1.jpg" target="_blank">点击查看示例</a></div>';
        }, '');
        $form->table('admin_auth_map','映射组', function (NestedForm $table) {
            $table->text('id', 'id')->default('map001')->required();
            $table->keyValue('circleMap', '圈子映射')->setKeyLabel('圈子id')->setValueLabel('分类id');
            $table->keyValue('goodsClassMap', '商品分类映射')->setKeyLabel('商品分类id')->setValueLabel('商品分类id');
        })->default(Settings::get('admin_auth_map', ''));
        $form->html(function () {
            return '';
        }, '<b>二、同步列表</b>');
        $form->table('admin_content_source1','LightSNS', function (NestedForm $table) {
            $table->text('url', '地址')->default('https://minisns.cn');
            $table->text('safe', '安全后缀')->default('abc=123');
            $table->switch('needSync', '同步至')->default(false);
            $table->text('map', '映射组')->default('map001')->required();
        })->default(Settings::get('admin_content_source1', ''));
        $form->html(function () {
            return '<a href="https://minisns.cn/25292.html" target="_blank" style="color:steelblue;">下载LightSNS同步函数模块？</a><br><br>';
        }, '');
        $form->table('admin_content_source2','discuz', function (NestedForm $table) {
            $table->text('url', '测试')->default('https://minisns.cn');
        })->default(Settings::get('admin_content_source2', ''));
        $form->html(function () {
            return '<a href="#" target="_blank" style="color:steelblue;">下载Discuz同步插件？</a><br><br>';
        }, '');


        $form->action('settings/action/contents');
        return $form;
    }

    public function contents(Request $request){
        SiteUtils::forget_config_data();
        // 内容源
        Settings::set('admin_auth_code', $request->input('admin_auth_code'), true);
        Settings::set('admin_auth_map', $this->_table_keyvalue_data($request->input('admin_auth_map')), true);
        Settings::set('admin_content_source1', AdminUtils::_table_data($request->input('admin_content_source1')), true);
        Settings::set('admin_content_source2', AdminUtils::_table_data($request->input('admin_content_source2')), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
